$(document).ready(function(){
	
	$('#addToCartBtn').click(function(){
		var bid = $('#bookid').val();
		var num = $('#booknum').val();
		doPost(getContextPath()+'/cart/add/'+bid+'-'+num, null, function(data){
			if (data.errorCode == '0') {
				showMsg('加入购物车成功。',true);
			} else {
				showMsg('加入购物车失败，请重新操作。',true);
			}
		});
	});
	
	$('button[id^=delFromCartBtn]').click(function(){
		var id = this.id.split('-')[1];
		confirmMsg('您确定要删除图书吗？',function(){
			doPost(getContextPath()+'/cart/remove/'+id, null, function(){
				window.location.reload();
			});
		});
	});
	
	$('#subOrderBtn').click(function(){
		if (isValidateForm()) {
			doPost(getContextPath()+'/order/submit', $('form:first').serialize(), function(data){
				if (data.errorCode == '0') {
					showMsg('提交订单成功。',true,function(){
						window.location.href = getContextPath()+'/order/list';
					});
				}else if (data.errorCode == '2001') {
					showMsg('提交订单失败，库库不足。',false);
				} else {
					showMsg('提交订单失败，请重新操作。',false);
				}
			});
		}
	});
	$('button[id^=buySoldBtn]').click(function(){
		var id = this.id.split('-')[1];
		doPost(getContextPath()+'/trade/getsolddetail/'+id, null, function(data){
			if (data.errorCode == '0') {
				var trade = data.data.trade;
				var book = data.data.book;
				var user = data.data.user;
				$('.buyBook-modal #name').text(book.name);
				$('.buyBook-modal #price').text(trade.price);
				$('.buyBook-modal #num').text(trade.num);
				$('.buyBook-modal #total').text(trade.total);
				$('.buyBook-modal #soldTotal').text(trade.sold_total);
				
				$('.buyBook-modal #buyerName').val(user.name);
				$('.buyBook-modal #buyerMobile').val(user.mobile);
				$('.buyBook-modal #buyerAddress').val(user.address);
				$('.buyBook-modal #id').val(id);
			}else if (data.errorCode == '1002') {
				showMsg('提交订单失败，库库不足。',false);
			} else {
				showMsg('获取数据失败，请重新操作。',false);
			}
		});
		
	});
	
	$('button[id^=buyExchBtn]').click(function(){
		var id = this.id.split('-')[1];
		var num = this.id.split('-')[2];
		doPost(getContextPath()+'/trade/getexchdetail/'+id, null, function(data){
			if (data.errorCode == '0') {
				var items = data.data.items;
				if (items.length == 0) {
					showMsg('您没有合适的图书换购。', false);
					return;
				} 
				var table = '<table class="table"><thead><th></th><th>书名</th><th>单价</th><th>数量</th><th>总价</th></thead>';
				for(var i = 0;i < items.length;i++) {
					var item = items[0];
					table += '<tr><td><label><input type="radio" name="orderItemId" checked value="'+item.id+'">&nbsp;选择此书换购</label></td>';
					table += '<td>'+data.data['book_'+item.id].name+'</td>';
					table += '<td>'+item.price+'</td>';
					table += '<td>'+num+'</td>';
					table += '<td>'+item.price * num +'</td></tr>';
				}
				table += '</table>';
			
				$('#exchBookDiv').html(table);
				
				
				var user = data.data.user;
				
				$('.buyBook-modal #name').val(user.name);
				$('.buyBook-modal #mobile').val(user.mobile);
				$('.buyBook-modal #address').val(user.address);
				$('.buyBook-modal #tradeId').val(id);
				
				$('.buyBook-modal').modal('show');
			}else if (data.errorCode == '1002') {
				showMsg('提交订单失败，库库不足。',false);
			} else {
				showMsg('获取数据失败，请重新操作。',false);
			}
		});
		
	});
	
	$('#soldBookBtn').click(function(){
		doPost(getContextPath()+'/trade/buysold', $('#soldBookForm').serialize(), function(data){
			if (data.errorCode == '0') {
				showMsg('购买成功，请耐心等待卖家发货。',true,function(){
					window.location.reload();
				});
			}else if (data.errorCode == '3001') {
				showMsg('购买失败，余额不足。',false);
			} else {
				showMsg('操作失败，请重新操作。',false);
			}
		});
		
	});
	
	$('#exchBookBtn').click(function(){
		doPost(getContextPath()+'/trade/buyexch', $('#soldBookForm').serialize(), function(data){
			if (data.errorCode == '0') {
				showMsg('换购成功，请耐心等待卖家发货。',true,function(){
					window.location.reload();
				});
			} else {
				showMsg('操作失败，请重新操作。',false);
			}
		});
		
	});
	
});